unit MatSelect;

interface

uses
  Winapi.Windows, Winapi.Messages,
  System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
  Vcl.ExtCtrls, Vcl.ExtDlgs,
   
  GLS.Texture, GLS.Material, Winapi.OpenGL, GLS.OpenGLTokens, GLS.Utils;

type
  TMatSelectForm = class(TForm)
    ListBox1: TListBox;
    Panel1: TPanel;
    Image1: TImage;
    Button1: TButton;
    Button2: TButton;
    OpenPictureDialog1: TOpenPictureDialog;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure ListBox1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure ListBox1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
     
  public
     
  end;

var
  MatSelectForm: TMatSelectForm;

//----------------------------------------------------------------------
implementation
//----------------------------------------------------------------------

uses
  Main;

{$R *.dfm}

procedure TMatSelectForm.Button1Click(Sender: TObject);
begin
  // if listbox1.ItemIndex=-1 then
  // modalresult:=mrnone;
end;

procedure TMatSelectForm.Button3Click(Sender: TObject);
var
  mname: string;
begin
  if OpenPictureDialog1.execute then
  begin
    mname := extractfilename(extractfilename(OpenPictureDialog1.filename));
    mname := copy(mname, 1, pos('.', mname) - 1);
    form1.matfilenames.add(OpenPictureDialog1.filename);
    with form1.glmateriallibrary1.AddTextureMaterial(mname,
      OpenPictureDialog1.filename) do
    begin
      material.Texture.MinFilter := miLinear;
      material.Texture.MagFilter := maLinear;
    end;
    ListBox1.Items.add(mname);
    ListBox1.ItemIndex := ListBox1.Items.count - 1;
    ListBox1Click(self);
  end;
end;

procedure TMatSelectForm.FormShow(Sender: TObject);
begin
  (* Form1.GLMaterialLibrary1.Materials.SetNamesToTStrings(listbox1.Items); *)
  { if listbox1.Count>0 then
    begin
    listbox1.ItemIndex:=0;
    ListBox1Click(self);
    end; }
end;

procedure TMatSelectForm.ListBox1Click(Sender: TObject);
var
  mat: TGLMaterial;
  bm: TBitmap;
begin
  if ListBox1.ItemIndex > -1 then
  begin
    mat := form1.glmateriallibrary1.materials[ListBox1.ItemIndex].material;
    bm := mat.Texture.Image.GetBitmap32.Create32BitsBitmap;
    Image1.picture.Assign(bm);
    bm.free;
  end;
end;

procedure TMatSelectForm.ListBox1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = vk_delete then
  begin
    if ListBox1.ItemIndex > -1 then
    begin
      form1.glmateriallibrary1.materials.Delete(ListBox1.ItemIndex);
      form1.matfilenames.Delete(ListBox1.ItemIndex);
      ListBox1.Items.Delete(ListBox1.ItemIndex);
      Image1.picture.Assign(nil);
    end;
  end;
end;

procedure TMatSelectForm.Button4Click(Sender: TObject);
begin
  if ListBox1.ItemIndex > -1 then
  begin
    form1.glmateriallibrary1.materials.Delete(ListBox1.ItemIndex);
    form1.matfilenames.Delete(ListBox1.ItemIndex);
    ListBox1.Items.Delete(ListBox1.ItemIndex);
    // listbox1.DeleteSelected;
    Image1.picture.Assign(nil);
  end;
end;

end.
